import 'package:flutter/material.dart';

typedef SliverGridItemBuilder<T> =
    Widget Function(BuildContext context, T item, int index);

class CommonSliverGrid<T> extends StatelessWidget {
  final List<T> items;
  final int crossAxisCount;
  final double mainAxisSpacing;
  final double crossAxisSpacing;
  final double childAspectRatio;
  final EdgeInsetsGeometry? padding;
  final SliverGridItemBuilder<T> itemBuilder;

  const CommonSliverGrid({
    super.key,
    required this.items,
    required this.itemBuilder,
    this.crossAxisCount = 5,
    this.mainAxisSpacing = 18,
    this.crossAxisSpacing = 30,
    this.childAspectRatio = 0.81,
    this.padding,
  });

  @override
  Widget build(BuildContext context) {
    return SliverPadding(
      padding: padding ?? EdgeInsets.zero,
      sliver: SliverGrid(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: crossAxisCount,
          mainAxisSpacing: mainAxisSpacing,
          crossAxisSpacing: crossAxisSpacing,
          childAspectRatio: childAspectRatio,
        ),
        delegate: SliverChildBuilderDelegate((context, index) {
          final item = items[index];
          return itemBuilder(context, item, index);
        }, childCount: items.length),
      ),
    );
  }
}
